package com.gzhryc.shared_device.oem.h5.mng.controllers.agent;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentFuncModel;
import com.gzhryc.shared_device.oem.code.agent.services.AgentFuncModelService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class AgentFuncModelController {

	@Route(value = "/agent/func_model/list.data")
    public ListResult<AgentFuncModel> funcModelListData(HttpServletRequest request) {
        ListResult<AgentFuncModel> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long userId = WebUtils.getLong(request,"userId");
        result.getBody().setData(AgentFuncModelService.self().findJoinByUserId(userId));
        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
        	String resDomain = WebConstants.getResDomain(request);
            for(AgentFuncModel item : result.getBody().getData()){
                if(StringTools.isNotBlank(item.getFuncModel().getIconUrl())){
                    item.getFuncModel().setIconUrl(resDomain + item.getFuncModel().getIconUrl());
                }
            }
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }
	
	@Route(value = { "/agent/func_model/add.html" })
    public String addFuncModel(HttpServletRequest request) {
        return "/agent/edit_func_model.html";
    }
	
	@Route(value = { "/agent/func_model/add.do" })
    public AjaxResult doAddFuncModel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        AgentFuncModel userFuncModel = WebUtils.getParameter(request, AgentFuncModel.class);
        try {
            if(AgentFuncModelService.self().add(userFuncModel)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
	
	@Route(value = { "/agent/func_model/del.do" })
    public AjaxResult doDelFuncModel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Long userId = WebUtils.getLong(request,"userId");
        Long funcModelId = WebUtils.getLong(request,"funcModelId");
        try {
            if(AgentFuncModelService.self().deleteByKeys(userId,funcModelId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
